home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Auge 4000 / Auge 4000 #06 (1987-05-23)(Amiga User Gruppe Einzugsgebiet 4000).zip / Auge 4000 #06 (1987-05-23)(Amiga User Gruppe Einzugsgebiet 4000).adf / Tips&Tricks / setfont.c < prev    next >
C/C++ Source or Header  |  1987-03-08  |  5KB  |  213 lines

  1. /*
  2.         Find CON: window pointer and set its font
  3.         by A. Finkel, R. Burns, and M. McInerny
  4.         )1986 by Commodore-Amiga and M. McInerny.
  5. */
  6.  
  7. #include    "exec/types.h"
  8. #include    "exec/nodes.h"
  9. #include    "exec/ports.h"
  10. #include    "exec/io.h"
  11. #include    "exec/memory.h"
  12. #include    "devices/console.h"
  13. #include    "devices/conunit.h"
  14. #include    "libraries/dos.h"
  15. #include    "libraries/dosextens.h"
  16. #include    "intuition/intuitionbase.h"
  17. #include    "workbench/startup.h"
  18. #include    "workbench/workbench.h"
  19. #include    "graphics/rastport.h"
  20. #include    "graphics/text.h"
  21. #include    "libraries/diskfont.h"
  22.  
  23. extern struct Library *OpenLibrary();
  24. extern struct TextFont *OpenDiskFont();
  25.  
  26. struct IntuitionBase *IntuitionBase = 0;
  27. long GfxBase = 0;
  28. long DiskfontBase = 0;
  29. struct TextFont *font;
  30.  
  31. struct MsgPort iorp = {
  32.     {0, 0, NT_MSGPORT, 0, 0}, 0,
  33.     -1,                /* initialize signal to -1 */
  34.     0,                /* start with empty list */
  35.     {(struct Node *)&iorp.mp_MsgList.lh_Tail, 0, 
  36.      (struct Node *)&iorp.mp_MsgList.lh_Head, 0, 0}
  37. };
  38. struct IOStdReq ior = {
  39.     {{0, 0, 0, 0, 0}, &iorp, 0},
  40.     0                /* device is zero */
  41. };
  42.  
  43. void OpenLibs()
  44. {
  45.     GfxBase = (long)OpenLibrary("graphics.library", 0);
  46.     if (GfxBase == NULL) {
  47.         printf("Graphics library open failed.\n");
  48.         cleanup(20);
  49.     }
  50.  
  51.     if ((IntuitionBase = (struct IntuitionBase *)
  52.                 OpenLibrary("intuition.library", 0)) == 0) {
  53.         cleanup(21);
  54.     }
  55.  
  56.     DiskfontBase = (long)OpenLibrary("diskfont.library", 0);
  57.     if (DiskfontBase == NULL) {
  58.         printf("Diskfont library open failed.\n");
  59.         cleanup(25);
  60.     }
  61.  
  62. }
  63.  
  64. struct Window *GetWindow()
  65. {
  66.     struct MsgPort *con;
  67.     struct StandardPacket *packet = 0;
  68.     struct InfoData *id = 0;
  69.     struct Window *windw = 0;
  70.  
  71.     /* open the console device, returns address of device handler */
  72.     if ((OpenDevice("console.device", -1, &ior, 0)) != 0) {
  73.         cleanup(30);
  74.     }
  75.  
  76.     /* set up the message port in the I/O request */
  77.     if ((iorp.mp_SigBit = AllocSignal(-1)) < 0) {
  78.         cleanup(35);
  79.     }
  80.  
  81.     /* which task am I? */
  82.     iorp.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  83.  
  84.     /* Try to find the console associated with calling process */
  85.     if (iorp.mp_SigTask->tc_Node.ln_Type == NT_PROCESS) {
  86.        con = (struct MsgPort *)
  87.           ((struct Process *) iorp.mp_SigTask)->pr_ConsoleTask;
  88.        if (con != 0) {
  89.           if (packet = (struct StandardPacket *)
  90.                AllocMem(sizeof(*packet), MEMF_CLEAR)) {
  91.            if (id = (struct InfoData *)AllocMem(sizeof(*id), MEMF_CLEAR)) {
  92.          /* This is the console handlers packet port. */
  93.          packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
  94.          packet->sp_Pkt.dp_Link = (struct Message *)&(packet->sp_Pkt);
  95.          packet->sp_Pkt.dp_Port = &iorp;
  96.          packet->sp_Pkt.dp_Type = ACTION_DISK_INFO;
  97.          packet->sp_Pkt.dp_Arg1 = ((ULONG) id) >> 2; /* #@!% BPTR! */
  98.          PutMsg(con, packet);
  99.          WaitPort(&iorp);
  100.          windw = (struct Window *) (id->id_VolumeNode);
  101.          FreeMem(id, sizeof(*id));
  102.            }
  103.            FreeMem(packet, sizeof(*packet));
  104.          }
  105.       }
  106.     }
  107.     ior.io_Unit = (struct Unit *) -1;
  108.     return(windw);
  109. }
  110.  
  111. cleanup(code)
  112. int code;
  113. {
  114.     if (ior.io_Device != 0) {
  115.         if (iorp.mp_SigBit != -1) {
  116.             FreeSignal(iorp.mp_SigBit);
  117.         }
  118.         CloseDevice(&ior);
  119.     }
  120.     if (font) CloseFont(font);
  121.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  122.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  123.     if (GfxBase) CloseLibrary(GfxBase);
  124.  
  125.     exit(code);
  126. }
  127.  
  128. main(argc, argv)
  129. int argc;
  130. char *argv[];
  131. {
  132.     struct TextAttr *ta = 0;
  133.     struct TextAttr *old_ta;
  134.     struct TextFont *oldfont;
  135.     struct Window *win;
  136.     struct RastPort *rp;
  137.     int fontsize = 8;
  138.     char *fontname;
  139.     int    i,
  140.         screen_flag = 0,
  141.         rast_flag = 0;
  142.  
  143.     if (*argv[1] == '?')
  144.     {    
  145.         printf(
  146. "Usage:  %s [fontname=topaz [fontsize=8]] [-s=set screen] [-r=set rastport]\n",
  147.             argv[0]);
  148.         printf("v1.1 )1986 by M. McInerny and Commodore-Amiga.\n");
  149.         cleanup(10);
  150.     }
  151.  
  152. #define FBUFSIZE 40 
  153.     fontname = (char *)AllocMem(FBUFSIZE, MEMF_CLEAR);
  154.     if (argc>1) {
  155.         fontname = (char *)strncpy(fontname, argv[1], FBUFSIZE);
  156.         fontname = (char *)strncat(fontname, ".font", FBUFSIZE);
  157.     } else {
  158.         fontname = (char *)strncpy(fontname, "topaz.font", FBUFSIZE);
  159.         screen_flag = 1;    /* restore the screen to normal */
  160.         rast_flag = 1;
  161.     }
  162.  
  163.     if ((argc>2) && (*argv[2] != '-')) fontsize = atoi(argv[2]);
  164.  
  165.     for (i = 1; i < argc; ++i)
  166.         if (*argv[i] == '-')
  167.             switch (*(argv[i]+1)) {
  168.                 case 's': screen_flag = 1; break;
  169.                 case 'r': rast_flag = 1; break;
  170.             }
  171.     OpenLibs();
  172.  
  173.     ta = (struct TextAttr *)AllocMem(sizeof(*ta), MEMF_CLEAR);
  174.     ta->ta_Name =  fontname;
  175.     ta->ta_YSize = fontsize;
  176.     ta->ta_Style = 0;
  177.     ta->ta_Flags = FPF_DISKFONT;
  178.  
  179.     font = OpenDiskFont(ta);
  180.     if (font == 0)
  181.     {
  182.         printf("OpenDiskFont failed--font not found?\n");
  183.          cleanup(27);
  184.     }
  185.  
  186.     win = GetWindow();
  187.     if (!win) cleanup(40);
  188.  
  189.     if (screen_flag == 1) {
  190.         old_ta = win->WScreen->Font;
  191.         win->WScreen->Font = ta;
  192.         FreeMem(old_ta, sizeof(old_ta));
  193.     } else FreeMem(ta, sizeof(ta));
  194.  
  195.     if (rast_flag == 1) {
  196.         rp = &(win->WScreen->RastPort);
  197.         oldfont =  rp->Font;
  198.         if (SetFont(rp, font) != 0)
  199.             CloseFont(oldfont);
  200.     }
  201.  
  202.     rp = win->RPort;
  203.     oldfont = rp->Font;
  204.     /* If the SetFont() works, on cleanup, CloseFont(oldfont) */
  205.     if (SetFont(rp, font) != 0) {
  206.         font = oldfont;
  207.         printf("\033c");
  208.     }
  209.     cleanup(0);
  210. }
  211.  
  212.  
  213.